import os
import switch_control.telnetAPI as telnetAPI
import time
import threading
import gpu_port_ip_map as Port_ip_map

gpu_spine_port_map = {}
spine_port_gpu_map = {}
vpn_spine_port_map = {}
gpu_vpn_map = {}
vrf_list = ["vrf1", "vrf2", "vrf3", "vrf4", "vrf5", "vrf6", "vrf7", "vrf8"]
GPU_NUM = 64
SERVER_GPU_NUM = 8
comm_list = []

PORT_PER_VPN = 8

def generate_spine_static_route(host_ip = '10.174.216.48'):
    username = 'admin1234'
    password = 'Oxc_2012'
    changepwd = 'N'

    commit = 'commit'
    quit = 'q'
    sy = "system-view"
    admin = "admin"
    port_mode = "port-mode port"
    confirm = "Y"

    comm_list.append(sy)

    for vpn_id in range(8):
        if vpn_id not in vpn_spine_port_map:
            vpn_spine_port_map[vpn_id] = []
        for i in range(4):
            vpn_spine_port_map[vpn_id].append(2*(i*8+vpn_id))
            vpn_spine_port_map[vpn_id].append(2*(i*8+vpn_id)+1)


    for gpu_id in range(GPU_NUM):
        logic_spine_id = int(gpu_id/2)%8
        logic_spine_local_port_num = 2*int(gpu_id/16)+gpu_id%2
        global_spine_port_id = vpn_spine_port_map[logic_spine_id][logic_spine_local_port_num]
        # print(logic_spine_local_port_num,global_spine_port_id)
        gpu_spine_port_map[gpu_id] = global_spine_port_id
        spine_port_gpu_map[global_spine_port_id]=gpu_id

    # for gpu_id in gpu_spine_port_map:
    #     print(gpu_id, gpu_spine_port_map[gpu_id])

    # for i in range(len(vpn_spine_port_map)):
    #     print( vpn_spine_port_map[i])

    for vpn_id in range(8):
        for spine_port_id in vpn_spine_port_map[vpn_id]:
            if spine_port_id%2 == 0:
                if int(spine_port_gpu_map[spine_port_id])>=32:
                    print(vpn_id+1, int(spine_port_id/2)+1, int(spine_port_gpu_map[spine_port_id]/8), Port_ip_map.find_gpu_famlily_ip(int(spine_port_gpu_map[spine_port_id])), Port_ip_map.find_hw_leaf_ip(spine_port_id%32))
                    cmd = "ip route-static vpn-instance %s %s 255.255.255.0 %s" %(vrf_list[vpn_id], Port_ip_map.find_gpu_famlily_ip(int(spine_port_gpu_map[spine_port_id])), Port_ip_map.find_hw_leaf_ip(spine_port_id%32) )
                    cmd2 = "ip route-static vpn-instance %s %s 255.255.255.0 %s" %(vrf_list[vpn_id], Port_ip_map.find_gpu_famlily_ip_copy(int(spine_port_gpu_map[spine_port_id])), Port_ip_map.find_hw_leaf_ip(spine_port_id%32) )
                else:
                    print(vpn_id+1, int(spine_port_id/2)+1, int(spine_port_gpu_map[spine_port_id]/8), Port_ip_map.find_gpu_famlily_ip(int(spine_port_gpu_map[spine_port_id])), Port_ip_map.find_rg_leaf_ip(spine_port_id%32))
                    cmd = "ip route-static vpn-instance %s %s 255.255.255.0 %s" %(vrf_list[vpn_id], Port_ip_map.find_gpu_famlily_ip(int(spine_port_gpu_map[spine_port_id])), Port_ip_map.find_rg_leaf_ip(spine_port_id%32) )
                    cmd2 = "ip route-static vpn-instance %s %s 255.255.255.0 %s" %(vrf_list[vpn_id], Port_ip_map.find_gpu_famlily_ip_copy(int(spine_port_gpu_map[spine_port_id])), Port_ip_map.find_rg_leaf_ip(spine_port_id%32) )
                comm_list.append(cmd)
                comm_list.append("commit")
                comm_list.append(cmd2)
                comm_list.append("commit")

    # for cmd in comm_list:
    #     print(cmd)

    # telnet_client = telnetAPI.TelnetClient()
    # if telnet_client.login_host(host_ip, username, password, changepwd):
    #     for cmd in comm_list:
    #         telnet_client.execute_some_command(cmd, interval=0.1)
    # else:
    #     print("fail")
    #     time.sleep(3)




generate_spine_static_route('10.174.216.48')